package demo2.demo2.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;

public class CxfClient2 {

    public static void main(String[] args) {
        CxfClient2.main3();
    }



    public static void main3() {
        Object retObj = null;

        Call call = null;

        Service service = new Service();

        //去掉缓存，以支持并发
        try {
            call = (Call) service.createCall();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        // 设置远程地址
//       call.setTargetEndpointAddress("http://127.0.0.1:8089/pb/myService/UserService");
        call.setTargetEndpointAddress("http://127.0.0.1:8089/pb/myService/PeopleService");

        // 清空操作
        call.clearOperation();
        // 清空头
        call.clearHeaders();

        Object[] body = new Object[]{"1"};
      //  body[0] = "sssssssssssssssssssss";

//        retObj = invokeMethod(call, "getUser", body);
        retObj = invokeMethod(call, "getPeople", body);

        System.out.println(retObj.toString());


    }



    private static Object invokeMethod(Call call, String opName,
                                Object... paras) {
        Object retObj = null;
        //设置要调用的方法
//        call.setOperationName(opName);
        call.setOperationName(new QName("http://service.demo2.demo2",opName));
//        call.addParameter("userId", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.addParameter("pId", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING);
        call.setUseSOAPAction(true);
        //该方法需要的参数
        Object[] objs = new Object[paras.length];
        int a = 0;
        for (Object objpara : paras) {
            objs[a] = objpara;
            a++;
        }
        try {
            //调用该方法
           retObj =  call.invoke(objs);
        } catch (Exception e) {
            throw new RuntimeException("原因："+e.getMessage(), e);
        }
        return   retObj;
    }

}
